package tu.java.menu;

import java.io.IOException;
import java.util.Vector;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import tu.java.login.User;

public class Menu {
	
	
	
	private String				href;
	private String				name;
	private String				accesskey;
	private String				cssclass;
	
	public static final String	GUEST_ACESS		= "0,1,2,3";
	public static final String	USER_ACESS		= "0,1,8";
	public static final String	CASHER_ACESS	= "0,1,6,8";
	public static final String	ADMIN_ACESS		= "0,1,3,4,5,7,8";
	
	
	
	public Menu(String name, String href, String accesskey, String cssclass) {
		this.name = name;
		this.href = href;
		this.accesskey = accesskey;
		this.cssclass = cssclass;
	}
	
	
	
	private static Vector<Menu> getMenus() {
		Vector<Menu> menus = new Vector<Menu>();
		menus.add(0, new Menu("Начало", "", "1", ""));
		menus.add(1, new Menu("Програма", "/program.jsp", "2", ""));
		menus.add(2, new Menu("Вход", "#login-box", "3", "login-window"));
		menus.add(3, new Menu("Регистрация", "#registration-box", "4", "login-window"));
		menus.add(4, new Menu("Филми", "/movies.jsp", "5", ""));
		menus.add(5, new Menu("Прожекции", "/projections.jsp", "6", ""));
		menus.add(6, new Menu("Чекиране", "/check.jsp", "7", "")); //ili neshto podobno като име
		menus.add(7,new Menu("Зали", "/halls.jsp", "8", ""));
		menus.add(8,new Menu("Изход", "/login/logOut.jsp", "9", ""));
		return menus;
	}
	
	
	
	public static String printMenus(HttpServletRequest request) {
		StringBuilder menuStr = new StringBuilder();
		Vector<Menu> menus = getMenus();
		HttpSession session = request.getSession(false);
		User user = (User) session.getAttribute("userObj");
		
		for (int i = 0; i < menus.size(); i++) {
			Menu m = menus.get(i);
			if (user.getUserAcess().contains(String.valueOf(i))) {
				if (i == 0) {
					menuStr.append("<li class=\"first active\">");
				}
				else {
					menuStr.append("<li>");
				}
				menuStr.append("<a href=\"");
				if (!m.href.contains("#")) {
					menuStr.append(request.getContextPath());
				}
				menuStr.append(m.href);
				menuStr.append("\" accesskey=\"");
				menuStr.append(m.accesskey);
				menuStr.append("\" class=\"");
				menuStr.append(m.cssclass);
				menuStr.append("\">");
				menuStr.append(m.name);
				menuStr.append("</a></li>\n");
			}
		}
		return menuStr.toString();
	}
	
	
	
	public static boolean checkUserAcess(HttpServletRequest request, int menuId) throws IOException {
		
		HttpSession session = request.getSession(false);
		User user = (User) session.getAttribute("userObj");
		if (!user.getUserAcess().contains(String.valueOf(menuId))) {				
			return false;
		}
		return true;
	}
	
	
	
	public String getHref() {
		return href;
	}
	
	
	
	public void setHref(String href) {
		this.href = href;
	}
	
	
	
	public String getName() {
		return name;
	}
	
	
	
	public void setName(String name) {
		this.name = name;
	}
	
	
	
	public String getAccesskey() {
		return accesskey;
	}
	
	
	
	public void setAccesskey(String accesskey) {
		this.accesskey = accesskey;
	}
	
}
